package com.itlb.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.itlb.entity.Admin;
import com.itlb.service.AdminService;


/**
 * 系统操作类控制器
 */
@Controller
@RequestMapping("/system")
public class LoginController {
	
	@Autowired
	private AdminService adminService;
	
	
	/**
	 * 系统登录后的主页
	 */
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public ModelAndView index(ModelAndView model){
		model.setViewName("system/index");
		return model;
	}
	
	/**
	 * 系统登录后的欢迎页
	 */
	@RequestMapping(value="/welcome",method=RequestMethod.GET)
	public ModelAndView welcome(ModelAndView model){
		model.setViewName("system/welcome");
		return model;
	}
	/**
	 * 登陆页面
	 */
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public ModelAndView login(ModelAndView model){
		model.setViewName("system/login");
		return model;
	}
	
	/**
	 * 登录表单提交处理控制器
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> loginAct(Admin admin,HttpServletRequest request){
		Map<String, String> ret = new HashMap<String, String>();
		if(admin == null){
			ret.put("type", "error");
			ret.put("msg", "请填写用户信息！");
			return ret;
		}
		if(StringUtils.isEmpty(admin.getAdminName())){
			ret.put("type", "error");
			ret.put("msg", "请填写用户名！");
			return ret;
		}
		if(StringUtils.isEmpty(admin.getPassword())){
			ret.put("type", "error");
			ret.put("msg", "请填写密码！");
			return ret;
		}
		Admin findByAdminName = adminService.findByAdminName(admin.getAdminName());
		if(findByAdminName == null){
			ret.put("type", "error");
			ret.put("msg", "该用户名不存在！");
			return ret;
		}
		if(!admin.getPassword().equals(findByAdminName.getPassword())){
			ret.put("type", "error");
			ret.put("msg", "密码错误！");
			return ret;
		}
		//说明用户名密码及验证码都正确
		//把角色信息、菜单信息放到session中
		request.getSession().setAttribute("admin", findByAdminName);
		ret.put("type", "success");
		ret.put("msg", "登录成功！");
		return ret;
	}
	
	/**
	 * 后台退出功能
	 */
	@RequestMapping(value="/logout",method=RequestMethod.GET)
	public String logout(HttpServletRequest request){
		HttpSession session = request.getSession();
		session.setAttribute("admin", null);
		return "redirect:login";
	}
	
	/**
	 * 修改密码页面
	 */
	@RequestMapping(value="/edit_password",method=RequestMethod.GET)
	public ModelAndView editPassword(ModelAndView model){
		model.setViewName("system/edit_password");
		return model;
	}
	
	@RequestMapping(value="/edit_password",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> editPasswordAct(String newpassword,String oldpassword,HttpServletRequest request){
		Map<String, String> ret = new HashMap<String, String>();
		if(StringUtils.isEmpty(newpassword)){
			ret.put("type", "error");
			ret.put("msg", "请填写新密码！");
			return ret;
		}
		Admin admin = (Admin)request.getSession().getAttribute("admin");
		if(!admin.getPassword().equals(oldpassword)){
			ret.put("type", "error");
			ret.put("msg", "原密码错误！");
			return ret;
		}
		admin.setPassword(newpassword);
		if(adminService.editPassword(admin) <= 0){
			ret.put("type", "error");
			ret.put("msg", "密码修改失败，请联系管理员！");
			return ret;
		}
		ret.put("type", "success");
		ret.put("msg", "密码修改成功！");
		return ret;
	} 
	

}
